/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.calculadora;

import akme.mobile.util.MathUtil;

/**
 *
 * @author Sidnei
 */
public class Computacao {

	public static int fromBinToDecimal(String bin){
		int result = 0;
        int temp = 0;
		int pot = 0;
		for (int x = bin.length()-1; x >= 0; x--){
			int pos = Integer.parseInt(String.valueOf(bin.charAt(x)));
			int base = (int) MathUtil.pow(2, pot);
			temp =  pos * base;
            result = result + temp;
			pot++;
		}
		return result;
	}

	public static String fromDecimalToBin(int num){
		StringBuffer result = new StringBuffer();

		do{
			if (num < 2){
				result.append("1");
				num = 0;
			} else{
				if (num % 2 > 0){
					result.append("1");
					num = num/2;
				} else {
					result.append("0");
					num = num/2;
				}
			}
		}while(num >= 1);
		result = result.reverse();
		return result.toString();
	}
	/*
	public static String fromBinToHex(String bin){

	}*/
}

